<template>
  <div class="detail-action">
    <div class="state">
      <span class="iconfont icon-order-unpay"></span>
      <p>{{orderStatus[order.orderState].label}}</p>
    </div>
    <div class="info">
      <p>订单编号：{{order.id}}</p>
      <p>下单时间：{{order.createTime}}</p>
    </div>
    <div class="btn">
      <!-- 待付款 -->
      <template v-if="order.orderState === 1">
        <MyButton @click="router.push('/member/pay?id='+order.id)" type="primary" size="small">立即付款</MyButton>
        <MyButton @click="handlerCancel(order)" type="gray" size="small">取消订单</MyButton>
      </template>
      <!-- 待发货 -->
      <template v-if="order.orderState === 2">
        <MyButton @click="router.push(`/member/checkout?orderId=${order.id}`)" type="primary" size="small">再次购买</MyButton>
      </template>
      <!-- 待收货 -->
      <template v-if="order.orderState === 3">
        <MyButton @click="handlerConfirm(order)" type="primary" size="small">确认收货</MyButton>
        <MyButton  @click="router.push(`/member/checkout?orderId=${order.id}`)" type="plain" size="small">再次购买</MyButton>
      </template>
      <!-- 待评价 -->
      <template v-if="order.orderState === 4">
        <MyButton  @click="router.push(`/member/checkout?orderId=${order.id}`)" type="primary" size="small">再次购买</MyButton>
        <MyButton type="plain" size="small">评价商品</MyButton>
        <MyButton type="gray" size="small">申请售后</MyButton>
      </template>
      <!-- 已完成 -->
      <template v-if="order.orderState === 5">
        <MyButton  @click="router.push(`/member/checkout?orderId=${order.id}`)" type="primary" size="small">再次购买</MyButton>
        <MyButton type="plain" size="small">查看评价</MyButton>
        <MyButton type="gray" size="small">申请售后</MyButton>
      </template>
      <!-- 已取消 -->
    </div>
    <Teleport to="#model">
      <OrderCancel ref="orderCancelCom" />
    </Teleport>
  </div>
</template>
<script setup>
import MyButton from '@/components/my-button'
import OrderCancel from './order-cancel'
import { useRouter } from 'vue-router'
import { orderStatus } from '@/api/constants'
import { defineProps, ref } from 'vue'
import Swal from 'sweetalert2'
  const router = useRouter()
  const props = defineProps({
    order: {
      type: Object,
      default: () => ({})
    }
  })
  // 组件实例
  const orderCancelCom = ref(null)
  // 取消订单
  const handlerCancel = (order) => {
    orderCancelCom.value.open(order)
  }
  // 确认收货
  const handlerConfirm = (order) => {
    Swal.fire({
      title:'收货',
      text:'您确认收到货吗？确认后货款将会打给卖家。',
      icon:'info',
      showCancelButton: true,
      confirmButtonColor: '#3085d6',
      cancelButtonColor: '#d33',
      confirmButtonText: '确定',
      cancelButtonText:'取消'
    }).then((result) => {
      if (result.value) {
        confirmOrder(order.id).then(() => {
          Swal.fire({
            title:'收货成功！',
            text:'确认收货成功！',
            icon:'success',
            showConfirmButton: false,
            timer: 1500
          })
         // 确认收货后状态变成 待评价
          order.orderState = 4
        })
      }
    })
  }
</script>
<style scoped lang="less">
.detail-action {
  height: 180px;
  width: 100%;
  display: flex;
  align-items: center;
  .state {
    width: 220px;
    text-align: center;
    .iconfont {
      font-size: 40px;
      color: @mainColor;
    }
    p {
      font-size: 16px;
      color: #666;
      margin-bottom: 10px;
    }
  }
  .info {
    width: 240px;
    line-height: 30px;
    p {
      color: #999;
    }
  }
  .btn {
    flex: 1;
    text-align: right;
    margin-right: 100px;
    .xtx-button {
      margin-left: 20px;
    }
  }
}
</style>